#!/bin/sh

check_return() {
    if [ $? == 0 ]; then
        echo -e "PASS: $1\n"
    else
        echo -e "FAIL: $1\n"
    fi
}

echo "-----------------------------------------------------------------------------------------------"
echo "Signing a template file..."
./sign1 sign1-tmpl.xml rsakey.pem > sign1-res.xml
check_return sign-tmpl

echo "-----------------------------------------------------------------------------------------------"
echo "Signing a template file with xmlsec1..."
xmlsec1 sign --privkey:rsakey.pem rsakey.pem --output sign1-res-xmlsec1.xml sign1-tmpl.xml
check_return sign-tmpl-xmlsec1

echo "-----------------------------------------------------------------------------------------------"
echo "Signing a dynamicaly created template..."
./sign2 sign2-doc.xml rsakey.pem > sign2-res.xml
check_return sign-dynamic-templ

echo "-----------------------------------------------------------------------------------------------"
echo "Signing a file with a dynamicaly created template and an X509 certificate..."
./sign3 sign3-doc.xml rsakey.pem rsacert.pem > sign3-res.xml
check_return sign-dynamic-templ-x509

echo "-----------------------------------------------------------------------------------------------"
echo "Signing a node in a file with a dynamicaly created template and an X509 certificate..."
./sign4 sign4-doc.xml "data" rsakey.pem rsacert.pem > sign4-res.xml
check_return sign-file-node-dynamic-templ-x509

echo "-----------------------------------------------------------------------------------------------"
echo "Verifying a signature with a single key..."
./verify1 sign1-res.xml rsapub.pem
check_return verify-single-key-1
./verify1 sign2-res.xml rsapub.pem
check_return verify-single-key-2

echo "-----------------------------------------------------------------------------------------------"
echo "Verifying a signature with keys manager..."
./verify2 sign1-res.xml rsakey.pem
check_return verify-keys-1-manager
./verify2 sign2-res.xml rsakey.pem
check_return verify-keys-2-manager

echo "-----------------------------------------------------------------------------------------------"
echo "Verifying a signature with xmlsec1..."
xmlsec1 verify --pubkey:rsakey.pem rsapub.pem sign1-res-xmlsec1.xml
check_return verify-keys-1-xmlsec1
xmlsec1 verify --pubkey:rsakey.pem rsapub.pem sign2-res.xml
check_return verify-keys-2-xmlsec1

echo "-----------------------------------------------------------------------------------------------"
echo "Verifying a signature with X509 certificates..."
./verify3 sign3-res.xml ca2cert.pem cacert.pem
check_return verify-x509

echo "-----------------------------------------------------------------------------------------------"
echo "Verifying a signature using X509 certificates with xmlsec1..."
xmlsec1 verify --untrusted ca2cert.pem --trusted cacert.pem sign3-res.xml
check_return verify-x509-xmlsec1

echo "-----------------------------------------------------------------------------------------------"
echo "Verifying a signature over a node using X509 certificate..."
./verify4 sign4-res.xml "data" ca2cert.pem cacert.pem
check_return verify-node-x509

echo "-----------------------------------------------------------------------------------------------"
echo "Verifying a signature over a node using X509 certificate with xmlsec1..."
xmlsec1 verify --add-id-attr ID --untrusted ca2cert.pem --trusted cacert.pem sign4-res.xml
check_return verify-node-x509-xmlsec1

echo "-----------------------------------------------------------------------------------------------"
echo "Verifying a simple SAML response using X509 certificate..."
./verify-saml verify-saml-res.xml ca2cert.pem cacert.pem
check_return verify-sampl-x509

echo "-----------------------------------------------------------------------------------------------"
echo "Verifying a simple SAML response using X509 certificate with xmlsec1..."
xmlsec1 verify --trusted ca2cert.pem --trusted cacert.pem verify-saml-res.xml
check_return verify-sampl-x509-xmlsec1

echo "-----------------------------------------------------------------------------------------------"
echo "Encrypting data with a template file..."
./encrypt1 encrypt1-tmpl.xml deskey.bin > encrypt1-res.xml
check_return encrypt-tmpl

echo "-----------------------------------------------------------------------------------------------"
echo "Encrypting data with a template file with xmlsec1..."
xmlsec1 encrypt --deskey:deskey.bin deskey.bin  --binary-data binary.dat --output encrypt1-res-xmlsec1.xml encrypt1-tmpl.xml
check_return encrypt-tmpl-xmlsec1

echo "-----------------------------------------------------------------------------------------------"
echo "Encrypting data with a dynamicaly created template..."
./encrypt2 encrypt2-doc.xml deskey.bin > encrypt2-res.xml
check_return encrypt-dynamic-tmpl

echo "-----------------------------------------------------------------------------------------------"
echo "Encrypting data with a session key..."
./encrypt3 encrypt3-doc.xml rsakey.pem > encrypt3-res.xml
check_return encrypt-session-key

echo "-----------------------------------------------------------------------------------------------"
echo "Decrypting data with a single key..."
./decrypt1 encrypt1-res.xml deskey.bin
check_return decrypt-single-key-1
./decrypt1 encrypt2-res.xml deskey.bin
check_return decrypt-single-key-2

echo "-----------------------------------------------------------------------------------------------"
echo "Decrypting data with keys manager..."
./decrypt2 encrypt1-res.xml deskey.bin
check_return decrypt-keys-1-manager
./decrypt2 encrypt2-res.xml deskey.bin
check_return decrypt-keys-2-manager

echo "-----------------------------------------------------------------------------------------------"
echo "Decrypting data with xmlsec1..."
xmlsec1 decrypt --deskey:deskey.bin deskey.bin encrypt1-res-xmlsec1.xml
check_return decrypt-key-1-xmlsec1
xmlsec1 decrypt --deskey:deskey.bin deskey.bin encrypt2-res.xml
check_return decrypt-key-2-xmlsec1
xmlsec1 decrypt --privkey:rsakey.pem rsakey.pem encrypt3-res.xml
check_return decrypt-key-3-xmlsec1

echo "-----------------------------------------------------------------------------------------------"
echo "Decrypting using custom keys manager..."
./decrypt3 encrypt1-res.xml
check_return decrypt-keys-1-manager
./decrypt3 encrypt2-res.xml
check_return decrypt-keys-2-manager
./decrypt3 encrypt3-res.xml
check_return decrypt-keys-3-manager
